package com.Xht.www.dao;

import com.Xht.www.po.UserCheckinPO;

import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class UserCheckinDAO extends BaseDAO {

    // 插入用户签到信息
    public boolean addUserCheckin(UserCheckinPO checkinPO) {
        String sql = "INSERT INTO user_checkin (user_id, checkin_time, points_earned) VALUES (?,?,?)";
        Connection connection = getConnection();
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, checkinPO.getUserId());
            pstmt.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
            pstmt.setInt(3, checkinPO.getPointsEarned());
            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            releaseConnection(connection);
        }
    }


    // 检查用户是否在24小时内签到过
    public boolean hasCheckedInWithin24Hours(int userId) {
        String sql = "SELECT checkin_time FROM user_checkin WHERE user_id = ? ORDER BY checkin_time DESC LIMIT 1";
        Connection connection = getConnection();
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    Timestamp lastCheckinTime = rs.getTimestamp("checkin_time");
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.HOUR_OF_DAY, -24);
                    Timestamp twentyFourHoursAgo = new Timestamp(calendar.getTimeInMillis());
                    return lastCheckinTime.after(twentyFourHoursAgo);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseConnection(connection);
        }
        return false;
    }

    // 获取用户上一次的签到时间
    public Timestamp getLastCheckinTime(int userId) {
        String sql = "SELECT checkin_time FROM user_checkin WHERE user_id = ? ORDER BY checkin_time DESC LIMIT 1";
        Connection connection = getConnection();
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getTimestamp("checkin_time");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseConnection(connection);
        }
        return null;
    }

    // 通过用户id获取签到历史
    public List<UserCheckinPO> getCheckinHistoryByUserId(int userId) {
        List<UserCheckinPO> checkinHistory = new ArrayList<>();
        String sql = "SELECT * FROM user_checkin WHERE user_id = ? ORDER BY checkin_time DESC";
        Connection connection = getConnection();
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    UserCheckinPO checkin = new UserCheckinPO();
                    checkin.setCheckinId(rs.getInt("checkin_id"));
                    checkin.setUserId(rs.getInt("user_id"));
                    checkin.setCheckinTime(rs.getTimestamp("checkin_time"));
                    checkin.setPointsEarned(rs.getInt("points_earned"));
                    checkinHistory.add(checkin);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseConnection(connection);
        }
        return checkinHistory;
    }
}
